package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("user")
public class SmsController {

    @Autowired
    private SmsService smsService;


    /**
     * 发送短信验证码接口：
     *
     * @param param  前端请求参数:phone -->使用map进行格式化
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<ErrorResult> checkSendCode(@RequestBody Map<String, String> param) {
        ErrorResult errorResult = null;
        String phone = null;
        try {
            phone = param.get("phone");
            errorResult = this.smsService.sendCheckMsg(phone);   // 调用SmsService进行短信发送验证码
            if (null == errorResult) {
                //返回空则代表：发送ok:
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送短信验证码失败~~~ phone:" + phone);
            errorResult = ErrorResult.builder().errCode("000002").errMessage("短信验证码发送失败").build();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);

    }
}
